package com.ruoyi.web.controller.dictionary;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.SysMatType;
import com.ruoyi.service.ISysMatTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 材料类型Controller
 * 
 * @author ruoyi
 * @date 2020-07-03
 */
@Controller
@RequestMapping("/ruoyi/type")
public class SysMatTypeController extends BaseController
{
    private String prefix = "ruoyi/type";

    @Autowired
    private ISysMatTypeService sysMatTypeService;

    @RequiresPermissions("ruoyi:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询材料类型列表
     */
    @RequiresPermissions("ruoyi:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMatType sysMatType)
    {
        startPage();
        List<SysMatType> list = sysMatTypeService.selectSysMatTypeList(sysMatType);
        return getDataTable(list);
    }

    /**
     * 导出材料类型列表
     */
    @RequiresPermissions("ruoyi:type:export")
    @Log(title = "材料类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMatType sysMatType)
    {
        List<SysMatType> list = sysMatTypeService.selectSysMatTypeList(sysMatType);
        ExcelUtil<SysMatType> util = new ExcelUtil<SysMatType>(SysMatType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增材料类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存材料类型
     */
    @RequiresPermissions("ruoyi:type:add")
    @Log(title = "材料类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMatType sysMatType)
    {
        //获取创建者
        sysMatType.setMatCreateBy(ShiroUtils.getLoginName());
        //获取创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(sysMatTypeService.insertSysMatType(sysMatType));
    }

    /**
     * 修改材料类型
     */
    @GetMapping("/edit/{matId}")
    public String edit(@PathVariable("matId") Integer matId, ModelMap mmap)
    {
        SysMatType sysMatType = sysMatTypeService.selectSysMatTypeById(matId);
        mmap.put("sysMatType", sysMatType);
        return prefix + "/edit";
    }

    /**
     * 修改保存材料类型
     */
    @RequiresPermissions("ruoyi:type:edit")
    @Log(title = "材料类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMatType sysMatType)
    {
        //获取创建者
        sysMatType.setMatCreateBy(ShiroUtils.getLoginName());
        //获取创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        sysMatType.setMatType("sys_material");
        return toAjax(sysMatTypeService.updateSysMatType(sysMatType));
    }

    /**
     * 删除材料类型
     */
    @RequiresPermissions("ruoyi:type:remove")
    @Log(title = "材料类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMatTypeService.deleteSysMatTypeByIds(ids));
    }


}
